home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 11 / CU Amiga Magazine's Super CD-ROM 11 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-06].iso / cucd / graphics / mpimage / si / apack.asm next >
Assembly Source File  |  1995-04-06  |  8KB  |  251 lines

  1.  
  2. **************************************************************** 
  3. *  Copyright 1988 by CREATIVE FOCUS.  This code is freely 
  4. *  distributable as long as this notice is retained and no 
  5. *  other conditions are imposed upon its redistribution. 
  6. *  APACK.ASM --  
  7. *  A fully compatible replacement for Electronic Arts' PACKER.C 
  8. *  routine.  Converts data according to the IFF ILBM cmpByteRun1 
  9. *  compression protocol: 
  10. *     control bytes: 
  11. *        n =  0.. 127:   followed by n+1 bytes of data; 
  12. *        n = -1..-127:   followed by byte to be repeated -n+1 times; 
  13. *        n =     -128:   don't do no nada. 
  14. *     calling format: 
  15. *        long packrow(from, too, amt) 
  16. *           char **from, /* pointer to source data pointer */ 
  17. *                **too;  /* pointer to destination data pointer */ 
  18. *           long amt;    /* number of bytes to compress */ 
  19. *        return(number of bytes written to destination); 
  20. *     effects: 
  21. *         *from = *from + amt, and *too = *too + return; 
  22. *         return is "smart," that is, not greater than 
  23. *         MaxPackedSize = amt + ((amt+127) >> 7). 
  24. *     By commenting out CHECK (below) you disable checking for runs 
  25. *     exceeding 128 bytes.  That CHECK is not needed if you are sure 
  26. *     the amt to be compressed is always 128 or less. 
  27. *  !!! DISCLAIMER !!!  You use this code entirely at your own 
  28. *  risk.  I don't warrantee its fitness for any purpose.  I 
  29. *  can't even guarantee the accuracy of anything I've said 
  30. *  about it, though I've tried my damndest to get it right. 
  31. *  I may, in fact, be completely out of my tiny little mind :-). 
  32. *  That being said, I can be reached for questions, comments, 
  33. *  or concerns at: 
  34. *        Dr. Gerald Hull 
  35. *        CREATIVE FOCUS 
  36. *        12 White Street 
  37. *        Binghamton, N.Y.  13901 
  38. *        (607) 648-4082 
  39. *        bix:    ghull 
  40. *        PLink:  DRJERRY 
  41. *************************************************************** 
  42.  
  43.       xdef  _packrow 
  44.  
  45. PT    equr  a0                -> beginning of replicate run (if any) 
  46. IX    equr  a1                -> end+1 of input line 
  47. IP    equr  a2                -> beginning of literal run (if any) 
  48. IQ    equr  a3                -> end+1 of lit and/or rep run (if any) 
  49. OP    equr  a4                -> end+1 of output line current pos 
  50. FP    equr  a6                frame pointer 
  51. SP    equr  a7                stack pointer 
  52.  
  53. RT    equr  d0                return value 
  54. MX    equr  d1                check for maximum run = MAX 
  55. AM    equr  d2                amount 
  56. CH    equr  d3                character 
  57.  
  58. REGS  reg   AM/CH/IP/IQ/OP 
  59.  
  60. FRM   equ   8                 input line address 
  61. TOO   equ   12                output line address 
  62. AMT   equ   16                length of input line 
  63.  
  64. MAX   equ   128               maximum encodable output run 
  65. * CHECK equ   1                 turns on maximum row checking 
  66.  
  67.     Section    code,CODE
  68.  
  69. _packrow 
  70.  
  71.  
  72. ***************     CASE 0:   GRAB PARAMS & INITIALIZE 
  73. CAS0 
  74.       link     FP,#0 
  75.       movem.l  REGS,-(SP) 
  76.       movea.l  FRM(FP),IP 
  77.       movea.l  (IP),IP        IP = *from 
  78.       movea.l  IP,IQ          IQ = IP 
  79.       movea.l  IQ,IX 
  80.       adda.l   AMT(FP),IX     IX = IP + amt 
  81.       movea.l  TOO(FP),OP 
  82.       movea.l  (OP),OP        OP = *too 
  83.  
  84.  
  85. ***************     CASE 1:   LITERAL RUN 
  86. CAS1 
  87.       movea.l  IQ,PT          adjust PT (no replicates yet!) 
  88.       move.b   (IQ)+,CH       grab character 
  89.       cmpa.l   IQ,IX          if input is finished 
  90.       beq.s    CAS5              branch to case 5 
  91.  
  92.       ifd      CHECK 
  93.       move.l   IQ,MX 
  94.       sub.l    IP,MX 
  95.       cmpi     #MAX,MX        if run has reached MAX 
  96.       beq.s    CAS6              branch to case 6 
  97.       endc 
  98.  
  99.       cmp.b    (IQ),CH        if next character != CH 
  100.       bne.s    CAS1              stay in case 1 
  101.  
  102. *                             else fall into case 2 
  103.  
  104.  
  105. ***************     CASE 2:   AT LEAST 2 BYTE REPEAT 
  106. CAS2 
  107.       move.b   (IQ)+,CH       grab character 
  108.       cmpa.l   IQ,IX          if input is finished 
  109.       beq.s    CAS7              branch to case 7 
  110.  
  111.       ifd      CHECK 
  112.       move.l   IQ,MX 
  113.       sub.l    IP,MX 
  114.       cmpi     #MAX,MX        if run has reached MAX 
  115.       beq.s    CAS6              branch to case 6 
  116.       endc 
  117.  
  118.       cmp.b    (IQ),CH        if next character != CH 
  119.       bne.s    CAS1              branch to case 1 
  120.  
  121. *                             else fall into case 3 
  122.  
  123.  
  124. ***************     CASE 3:   REPLICATE RUN 
  125. CAS3 
  126.       move.b   (IQ)+,CH       grab character 
  127.       cmpa.l   IQ,IX          if input is finished 
  128.       beq.s    CAS7              branch to case 7 
  129.  
  130.       ifd      CHECK 
  131.       move.l   IQ,MX 
  132.       sub.l    PT,MX 
  133.       cmpi     #MAX,MX        if run has reached MAX 
  134.       beq.s    CAS4              branch to case 4 
  135.       endc 
  136.  
  137.       cmp.b    (IQ),CH        if next character = CH 
  138.       beq.s    CAS3              stay in case 3 
  139.  
  140. *                             else fall into case 4 
  141.  
  142.  
  143. ***************     CASE 4:   LIT AND/OR REP DUMP & CONTINUE 
  144. CAS4 
  145.       move.l   PT,AM 
  146.       sub.l    IP,AM          AM = PT - IP 
  147. *                             if no literal run 
  148.       beq.s    C41               branch to replicate run 
  149.  
  150.       subq     #1,AM          AM = AM - 1 
  151.       move.b   AM,(OP)+       output literal control byte 
  152.  
  153. C40   move.b   (IP)+,(OP)+    output literal run 
  154.       dbra     AM,C40 
  155.  
  156. C41   move.l   PT,AM 
  157.       sub.l    IQ,AM          AM = PT - IQ (negative result!) 
  158.       addq     #1,AM          AM = AM + 1 
  159.       move.b   AM,(OP)+       output replicate control byte 
  160.       move.b   CH,(OP)+       output repeated character 
  161.       movea.l  IQ,IP          reset IP 
  162.       bra.s    CAS1           branch to case 1 (not done) 
  163.  
  164.  
  165. ***************     CASE 5:   LITERAL DUMP & QUIT 
  166. CAS5 
  167.       move.l   IQ,AM 
  168.       sub.l    IP,AM          AM = IQ - IP (positive result > 0) 
  169.       subq     #1,AM          AM = AM - 1 
  170.       move.b   AM,(OP)+       output literal control byte 
  171.  
  172. C50   move.b   (IP)+,(OP)+    output literal run 
  173.       dbra     AM,C50 
  174.  
  175.       bra.s    CAS8           branch to case 8 (done) 
  176.  
  177.  
  178.       ifd      CHECK 
  179.  
  180. ***************     CASE 6:   LITERAL DUMP & CONTINUE 
  181. CAS6 
  182.       move.l   IQ,AM 
  183.       sub.l    IP,AM          AM = IQ - IP (positive result > 0) 
  184.       subq     #1,AM          AM = AM - 1 
  185.       move.b   AM,(OP)+       output literal control byte 
  186.  
  187. C60   move.b   (IP)+,(OP)+    output literal run 
  188.       dbra     AM,C60 
  189.  
  190.       bra      CAS1           branch to case 1 (not done) 
  191.  
  192.       endc 
  193.  
  194.  
  195. ***************     CASE 7:   LIT AND/OR REP DUMP & FINISH 
  196. CAS7 
  197.       move.l   PT,AM 
  198.       sub.l    IP,AM          AM = PT - IP (positive result > 0) 
  199. *                             if no literal run 
  200.       beq.s    C71               branch to replicate run 
  201.  
  202.       subq     #1,AM          AM = AM - 1 
  203.       move.b   AM,(OP)+       output literal control byte 
  204.  
  205. C70   move.b   (IP)+,(OP)+    output literal run 
  206.       dbra     AM,C70 
  207.  
  208. C71   move.l   PT,AM 
  209.       sub.l    IQ,AM          AM = PT - IQ (negative result) 
  210.       addq     #1,AM          AM = AM + 1 
  211.       move.b   AM,(OP)+       output replicate control byte 
  212.       move.b   CH,(OP)+       output repeated character 
  213.  
  214. *                             fall into case 8 
  215.  
  216.  
  217. ***************     CASE 8:   ADJUST PARAMS & RETURN VALUE 
  218. CAS8 
  219.       movea.l  FRM(FP),PT     PT = **from 
  220.       move.l   IQ,(PT)        *from = *from + amt 
  221.       movea.l  TOO(FP),PT     PT = **too 
  222.  
  223.       move.l   OP,RT 
  224.       sub.l    (PT),RT       return = OP - *too  
  225.  
  226.       move.l   OP,(PT)       *too = *too + return 
  227.       movem.l  (SP)+,REGS 
  228.       UNLK     FP 
  229.       rts 
  230.  
  231.       end
  232.  
  233.  
  234.